package org.microservice.system.listenter;

import org.microservice.common.bean.UserVo;
import org.microservice.common.util.constant.CacheConstant;
import org.microservice.common.util.constant.CommonConstant;
import org.microservice.common.util.constant.MqQueueConstant;
import org.microservice.redis.util.RedisService;
import org.microservice.system.service.SysUserService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/7/24
 * Time: 10:12
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Component
@RabbitListener(queues = MqQueueConstant.USER_DATA_SCOPE_CHANGE)
public class UserDataScopeReceiveListener {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private RedisService redisService;



    @RabbitHandler
    public void receive(String username){

        if(!username.equals(CommonConstant.ADMIN_NAME)){
            Map<String, List<Integer>> dataScope= sysUserService.getDataScopeByUserIdOrUserName(null,username);
            if(!redisService.exists(CacheConstant.USER_INFO_KEY + username)){
                redisService.set(CacheConstant.USER_INFO_KEY + username,new HashMap<String,Object>());
            }
            Map<String, Object> objectMap = (Map<String, Object>) redisService.get(CacheConstant.USER_INFO_KEY + username);

            objectMap.put(CacheConstant.USER_DATA_SCOPE_KEY,dataScope);

            redisService.set(CacheConstant.USER_INFO_KEY + username,objectMap);
        }
    }
}
